﻿using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace Fancy_Thumb
{
    public partial class Mainform : Form
    {

        bool IconChanged;
        bool LabelChanged;

        public Mainform()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DriveInfo Drive = new DriveInfo(Path.GetPathRoot(Environment.GetCommandLineArgs()[1]));
            if (LabelChanged)
            {
                Workers.SaveLabel(Drive, textBox1.Text);
            }
            Workers.SaveLabel(Drive, textBox1.Text);
            if (IconChanged)
                if (pictureBox1.Image!=null)         
                {
                    Workers.SaveIcon(Drive, openFileDialog1.FileName);
                }
                else
                    Workers.SaveIcon(Drive,null);
            Application.Exit();
        }

        private void Mainform_Shown(object sender, EventArgs e)
        {
            DriveInfo Drive = new DriveInfo(Path.GetPathRoot(Environment.GetCommandLineArgs()[1]));
            textBox1.Text = Workers.LoadLabel(Drive);
            String IconFile=Workers.LoadIcon(Drive);
            if (Drive.DriveType == DriveType.Fixed)
                label3.Text = label3.Text.Replace("reconnect the drive", "reboot");
            if (IconFile != null)
            {
                Icon IcoFile = new Icon(IconFile);
                pictureBox1.Image=IcoFile.ToBitmap();
                button4.Enabled = true;
                pictureBox1.BorderStyle = BorderStyle.None;
            }
            IconChanged= false;
            LabelChanged = false;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            pictureBox1.Image = null;
            button4.Enabled = false;
            IconChanged = true;
            pictureBox1.BorderStyle = BorderStyle.FixedSingle;
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            LabelChanged = true;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                pictureBox1.Image = new Icon(openFileDialog1.FileName).ToBitmap();
                IconChanged = true;
                button4.Enabled = true;
                pictureBox1.BorderStyle = BorderStyle.None;
            }
        }
    }
}
